package com.youth.yomapi.scanner;

import android.Manifest;
import android.content.Intent;

import com.example.xch.scanzxing.zxing.android.CaptureActivity;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.LOG;
import org.json.JSONArray;
import org.json.JSONException;

import static org.apache.cordova.device.Device.TAG;
public class ScannerPlugin extends CordovaPlugin {
    public static CallbackContext callbackContext;
    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        this.callbackContext = callbackContext;
        if("scan".equals(action)) {
            if(!cordova.hasPermission(Manifest.permission.CAMERA)){
                cordova.requestPermission(this,0,Manifest.permission.CAMERA);
            }else{
                startScanner();
            }
            return true;
        }
        return true;
    }
    private void startScanner(){
        Intent intent = new Intent(cordova.getActivity(), CaptureActivity.class);
        cordova.getActivity().startActivity(intent);
    }

    @Override
    public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
        if(requestCode == 0){
            startScanner();
        }
    }
}
